  <?php

  defined ('G_IN_SYSTEM') or exit ( 'No permission resources.' );

  System::load_app_class ( 'base', 'member', 'no' );

  System::load_app_fun ( 'user', 'go' );

  class cart extends base {

	 private $Cartlist;

	  

	  public function __construct() {

		  $this->Cartlist = _getcookie('Cartlist');

		
		  $this->db = System::load_sys_class("model");

  

	  }

  

  

	  //购物车商品列表
	  public function cartlist() {

		  $webname = $this->_cfg ['web_name'];
		  $Mcartlist = json_decode ( stripslashes ( $this->Cartlist ), true );

		  /*商品可用云购码处理 start yjmp*/
		  foreach ($Mcartlist as $k => $v) {
		  	$shopinfo = $this->db->GetOne(" select money,ygcode_list from `@#_shoplist` where `id` = '$k'");
		  	$Mcartlist[$k]['ygcode_list'] = $shopinfo['ygcode_list'];
		  	$Mcartlist[$k]['yjmoney'] = $shopinfo['money'];
		  }
		  $yjCartList = json_encode($Mcartlist);
		  /*商品可用云购码处理 end yjmp*/

		  $shopids = '';
		  if (is_array ( $Mcartlist )) {
			  foreach ( $Mcartlist as $key => $val ) {
				  $shopids .= intval ( $key ) . ',';
			  }

			  $shopids = str_replace ( ',0', '', $shopids );
			  $shopids = trim ( $shopids, ',' );
		  }

		  // echo $shopids;
		  $shoplist = array ();

		  if ($shopids != NULL) {
			  $shoparr = $this->db->GetList ( "SELECT * FROM `@#_shoplist` where `id` in($shopids)", array (
					"key" => "id" 
			  ) );

		  }

		  if (! empty ( $shoparr )) {
			  foreach ( $shoparr as $key => $val ) {
				  if ($val ['q_end_time'] == '' || $val ['q_end_time'] == NULL) {

					  $shoplist [$key] = $val;
					  $Mcartlist [$val ['id']] ['num'] = $Mcartlist [$val ['id']] ['num'];
					  $Mcartlist [$val ['id']] ['shenyu'] = $val ['shenyurenshu'];
					  $Mcartlist [$val ['id']] ['money'] = $val ['yunjiage'];

				  }
			  }
			  _setcookie ( 'Cartlist', json_encode ( $Mcartlist ), '' );

		  }

		  
		//当前在线用户
		$uid = intval(_encrypt(_getcookie("uid"), 'DECODE'));
		$members = $this->db->GetOne("select * from `@#_member` where uid='{$uid}'");
		  $MoenyCount = 0;
		  $Cartshopinfo = '{';

		  if (count ( $shoplist ) >= 1) {
			  foreach ( $Mcartlist as $key => $val ) {
				  $key = intval ( $key );
				  if (isset ( $shoplist [$key] )) {

					  $shoplist [$key] ['cart_gorenci'] = $val ['num'] ? $val ['num'] : 1;
					  $MoenyCount += $shoplist [$key] ['yunjiage'] * $shoplist [$key] ['cart_gorenci'];
					  $shoplist [$key] ['cart_xiaoji'] = substr ( sprintf ( "%.3f", $shoplist [$key] ['yunjiage'] * $val ['num'] ), 0, - 1 );
					  $shoplist [$key] ['cart_shenyu'] = $shoplist [$key] ['zongrenshu'] - $shoplist [$key] ['canyurenshu'];
					  $Cartshopinfo .= "'$key':{'shenyu':" . $shoplist [$key] ['cart_shenyu'] . ",'num':" . $val ['num'] . ",'money':" . $shoplist [$key] ['yunjiage'] . "},";
					  

						/* wk  获取当前用户是否有购买本商品 并统计数量 */
						$count = $this->db->GetOne("select sum(gonumber) as count from `@#_member_go_record` where `shopid`='".$shoplist[$key]['id']."' and shopqishu = '". $shoplist[$key]['qishu'] ."' and uid = '". $members['uid'] ."' ");

						$go_count = $count['count'];
						if($shoplist[$key]['limit_buy'] > 0){
							$shoplist[$key]['limit_buys'] = $shoplist[$key]['limit_buy'];
							$shoplist[$key]['limit_buy'] -= $go_count;
						}else{
							$shoplist[$key]['limit_buys'] = $shoplist[$key]['limit_buy'];
						}
						/* wk  获取当前用户是否有购买本商品 并统计数量 */
				  }

			  }

		  }

		  $shop = 0;

		  if (! empty ( $shoplist )) {
			  $shop = 1;
		  }
 
		  // echo "<pre>";

		  // print_r($Mcartlist);
		  
		  /*推荐商品*/
		  $rec_goods = $this->db->GetList(" select * from `@#_shoplist` where renqi = 1 and shenyurenshu > 0 and limit_buy = -1 order by id DESC limit 4 ");
		  // echo "<pre>";
		  // print_r($rec_goods);
		  // echo "</pre>";exit;
		  foreach ($rec_goods as $k => $v) {
		  	$rec_goods[$k]['buy_rate'] = intval($v['canyurenshu']/$v['zongrenshu']*100);
		  }

		  $MoenyCount = substr ( sprintf ( "%.3f", $MoenyCount ), 0, - 1 );

		  $Cartshopinfo .= "'MoenyCount':$MoenyCount}";

		  include templates ( "mobile/cart", "cartlist" );

	  }



	  

	  // 支付界面

	  public function pay() {

		  $webname = $this->_cfg ['web_name'];
		  parent::__construct ();

		  if (! $member = $this->userinfo) {
			  header ( "location: " . WEB_PATH . "/mobile/user/login" );
		  }
		  $Mcartlist = json_decode ( stripslashes ( $this->Cartlist ), true );
		  $shopids = '';

		  if (is_array ( $Mcartlist )) {
			  foreach ( $Mcartlist as $key => $val ) {
				  $shopids .= intval ( $key ) . ',';
			  }

			  $shopids = str_replace ( ',0', '', $shopids );
			  $shopids = trim ( $shopids, ',' );
		  }

		  $shoplist = array ();

		  if ($shopids != NULL) {
			  $shoplist = $this->db->GetList ( "SELECT * FROM `@#_shoplist` where `id` in($shopids)", array (
					  "key" => "id" 
			  ) );
		  }

		  $MoenyCount = 0;

		  if (count ( $shoplist ) >= 1) {
			  foreach ( $Mcartlist as $key => $val ) {

				  $key = intval ( $key );

				  if (isset ( $shoplist [$key] )) {

					  $shoplist [$key] ['cart_gorenci'] = $val ['num'] ? $val ['num'] : 1;
					  $MoenyCount += $shoplist [$key] ['yunjiage'] * $shoplist [$key] ['cart_gorenci'];
					  $shoplist [$key] ['cart_xiaoji'] = substr ( sprintf ( "%.3f", $shoplist [$key] ['yunjiage'] * $val ['num'] ), 0, - 1 );
					  $shoplist [$key] ['cart_shenyu'] = $shoplist [$key] ['zongrenshu'] - $shoplist [$key] ['canyurenshu'];

				  }
			  }

			  $shopnum = 0; // 表示有商品

		  } else {

			  _setcookie ( 'Cartlist', NULL );

			  // _message("购物车没有商品!",WEB_PATH);

			  $shopnum = 1; // 表示没有商品

		  }

		  // 总支付价格
		  $MoenyCount = substr ( sprintf ( "%.3f", $MoenyCount ), 0, - 1 );
		  // 会员余额
		  $Money = $member ['money'];
		  // 商品数量
		  $shoplen = count ( $shoplist );
		  $fufen = System::load_app_config ( "user_fufen", '', 'member' );

		  if ($fufen ['fufen_yuan']) {
			  $fufen_dikou = intval ( $member ['score'] / $fufen ['fufen_yuan'] );
		  } else {
			  $fufen_dikou = 0;
		  }

  // 		$paylist = $this->db->GetList ( "select * from `@#_pay` where `pay_start` = '1'" );
		  $paylist = $this->db->GetList("SELECT * FROM `@#_pay` where `pay_start` = '1' AND pay_mobile = 1");

		  session_start ();
		  $_SESSION ['submitcode'] = $submitcode = uniqid ();
		  include templates ( "mobile/cart", "payment" );
	  }

	  // 开始支付
	  public function paysubmit() {

		  $webname = $this->_cfg ['web_name'];
		  header ( "Cache-control: private" );
		  parent::__construct ();

		  if (! $this->userinfo) {
			  header ( "location: " . WEB_PATH . "/mobile/user/login" );
			  exit ();
		  }

		  session_start ();
		  $checkpay = $this->segment ( 4 ); // 获取支付方式 fufen money bank
		  $banktype = $this->segment ( 5 ); // 获取选择的银行 CMBCHINA ICBC CCB
		  $money = $this->segment ( 6 ); // 获取需支付金额
		  $fufen = $this->segment ( 7 ); // 获取积分

		  $submitcode1 = $this->segment ( 8 ); // 获取SESSION
		  $uid = $this->userinfo ['uid'];

		  if (! empty ( $submitcode1 )) {
			  if (isset ( $_SESSION ['submitcode'] )) {
				  $submitcode2 = $_SESSION ['submitcode'];
			  } else {
				  $submitcode2 = null;
			  }

			  if ($submitcode1 == $submitcode2) {
				  unset ( $_SESSION ["submitcode"] );
			  } else {
				  $WEB_PATH = WEB_PATH;
				  _messagemobile ( "请不要重复提交...<a href='{$WEB_PATH}/mobile/cart/cartlist' style='color:#22AAFF'>返回购物车</a>查看" );
				  exit ();
			  }
		  } else {
  // 			$WEB_PATH = WEB_PATH;
  // 			_messagemobile ( "正在返回购物车...<a href='{$WEB_PATH}/mobile/cart/cartlist' style='color:#22AAFF'>返回购物车</a>查看" );
		  }

		  $zhifutype = $this->db->GetOne ( "select * from `@#_pay` where `pay_class` = 'alipay' " );
		  if (! $zhifutype) {
			  _messagemobile ( "手机支付只支持易宝,请联系站长开通！" );
		  }

		  
		  $pay_checkbox = false;
		  $pay_type_bank = false;
		  $pay_type_id = false;

		  if ($checkpay == 'money') {
			  $pay_checkbox = true;
		  }

		  if ($banktype != 'nobank') {
			  $pay_type_id = $banktype;
		  }

		  if (! empty ( $zhifutype )) {
			  $pay_type_bank = $zhifutype ['pay_class'];
		  }

		  if (! $pay_type_id) {
			  if ($checkpay != 'fufen' && $checkpay != 'money')
				  _messagemobile ( "选择支付方式" );
		  }

		  /**

		   * ***********

		   * start

		   * ***********

		   */
		  $Cartlist=json_decode(stripslashes($this->Cartlist),true);

		  //判断购物车中的云购码是否被使用过 start yjmp
		$yj_err = 1;
		$err_msg = '';
		$temp_yjcode = array(); //保存购物车中的商品id和云购码后面用

		foreach ($Cartlist as $k => $v) {
		 	if (is_numeric($k)) {

		 		$temp_shop = array();
		 		$temp_shop['shopid'] = $k;
		 		
		 		$shopinfo = $this->db->GetOne(" select * from `@#_shoplist` where `id` = '$k' ");
		 		$code_arr = explode(',', $shopinfo['ygcode_list']);
		 		$user_code_arr = explode(',', $v['ygcode']);
				$temp_shop['code_arr'] = $user_code_arr;
				array_push($temp_yjcode, $temp_shop);

				for ($i=0; $i < count($user_code_arr); $i++) { 
					for ($j=0; $j < count($code_arr) ; $j++) { 
						if ($user_code_arr[$i] == $code_arr[$j]) {
							$yj_err = 0;
						}
					}

					if ($yj_err == 1) {
						$err_msg .= $user_code_arr[$i];
						$err_msg.= ',';
					}

					$yj_err = 1;
				}
		 	}
		}

		if ($err_msg != '') {
			$err_msg = '云购码：'.$err_msg.'已被使用';
			_messagemobile ( $err_msg."<a href='" . WEB_PATH . "/mobile/cart/cartlist' style='color:#22AAFF'>返回购物车</a>查看" );
		}
		//判断购物车中的云购码是否被使用过 end  yjmp

		  $pay = System::load_app_class ( 'pay', 'pay' );
		  $pay->fufen = $checkpay=='fufen'?$fufen:0;
		  $pay->pay_type_bank = $pay_type_bank;
		  $ok = $pay->init ( $uid, $pay_type_id, 'go_record' ); // 云购商品
		  if ($ok != 'ok') {
			  _setcookie ( 'Cartlist', NULL );
			  _messagemobile ( "购物车没有商品请<a href='" . WEB_PATH . "/mobile/cart/cartlist' style='color:#22AAFF'>返回购物车</a>查看" );
		  }

		  $check = $pay->go_pay ( $pay_checkbox );
		  if (! $check) {
			  _messagemobile ( "订单添加失败,请<a href='" . WEB_PATH . "/mobile/cart/cartlist' style='color:#22AAFF'>返回购物车</a>查看" );
		  }
		  if ($check) {
			// 成功
			//减去使用过的云购码start yjmp
			foreach ($temp_yjcode as $k => $v) {
	    		$org_info = $this->db->GetOne(" select ygcode_list from `@#_shoplist` where `id` = '$v[shopid]'");
	    		$org_code = explode(',', $org_info['ygcode_list']);
	    		for ($i=0; $i < count($org_code); $i++) { 
	    			for ($j=0; $j < count($v['code_arr']); $j++) { 
	    				if ($v['code_arr'][$j] == $org_code[$i]) {
	    					array_splice($org_code,$i,1,-1);
	    				}
	    			}
	    		}
	    		$new_code = '';
	    		foreach ($org_code as $key => $value) {
	    			if ($value != -1) {
	    				$new_code .= $value;
	    				$new_code .= ',';
	    			}
	    		}
	    		$yj_len = strlen($new_code);
	    		$new_code = substr($new_code, 0,$yj_len-1);
	    		$this->db->Query(" update `@#_shoplist` set ygcode_list = '$new_code' where id = '$v[shopid]'");
	    	}
			//减去使用过的云购码end yjmp
			  header ( "location: " . WEB_PATH . "/mobile/cart/paysuccess" );
		  } else {
			  // 失败
			  _setcookie ( 'Cartlist', NULL );
			  header ( "location: " . WEB_PATH . "/mobile/mobile" );
		  }
		  exit ();
	  }

	  

	

	//成功页面

	public function paysuccess(){

	    $webname=$this->_cfg['web_name'];

		_setcookie('Cartlist',NULL);

		include templates("mobile/cart","paysuccess");

	}






	  

	  // 充值

	  public function addmoney() {

		  parent::__construct ();

		  $webname = $this->_cfg ['web_name'];

		  $money = $this->segment ( 4 ); // 获取充值金额

		  $pay_id = $this->segment ( 5 ); // 获取选择的支付方式

		

		  if (! $this->userinfo) {

			  header ( "location: " . WEB_PATH . "/mobile/user/login" );

			  exit ();

		  }

		  

		  $payment = $this->db->GetOne ( "select * from `@#_pay` where `pay_id` = ".$pay_id );

		  

		  

		  if (! $payment) {

			  _messagemobile ( "对不起，没有您所选择的支付方式！" );

		  }

		  

		  if (! empty ( $payment )) {

			  $pay_type_bank = $payment ['pay_class'];

		  }

		  $pay_type_id = $pay_id;

  // 		$pay_type_bank=isset($_POST['pay_bank']) ? $_POST['pay_bank'] : false;

  // 		$pay_type_id=isset($_POST['account']) ? $_POST['account'] : false;

  // 		$money=intval($_POST['money']);

		  $uid = $this->userinfo ['uid'];

		  $pay = System::load_app_class ( 'pay', 'pay' );

		  $pay->pay_type_bank = $pay_type_bank;

		  $ok = $pay->init ( $uid, $pay_type_id, 'addmoney_record', $money );

  

		  if ($ok === 'not_pay') {

			  _messagemobile ( "未选择支付平台" );

		  }

	  }

  }

  

  ?>

